<?php

namespace admin\controllers;

use admin\components\BaseAuthController;
use app\helpers\ArrayHelper;
use app\models\AdminGroup;
use app\models\AdminMenu;

class UserGroupController extends BaseAuthController {

    public $name = '角色';

    public function actionIndex() {
        $model = new AdminGroup();
        $dataProvider = $model->search();
        return $this->render(compact('dataProvider'));
    }

    public function actionSave($id = FALSE) {
        $adminMenu = new AdminMenu();
        $lists = $adminMenu->access();
        $menus = ArrayHelper::arrayTreeIndex($lists);
        //print_r($menus);exit;

        /* @var $model AdminGroup */
        $model = $this->findModel($id, AdminGroup::className());
        $model->privilege_ids = $model->getPrivilegeIds();
        if (!$model->load($this->post())) {
            return $this->render(compact('model', 'menus'));
        }

        $this->logTitle = ($model->isNewRecord ? '添加' : '修改') . "【{$model->gname}】";

        return $model->save();
    }

}
